<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;

class Linkman extends Model
{
    //联系人的添加与编辑的判断
    public function addeditlinkman($data)
    {
        $ckdata = notEmpty(['name' => lang("name"), 'position' => lang("job")]);
        if ($ckdata['status'] == 2) {
            return $ckdata;
        }
        //判断联系方式
        if (!isset($data['linkarr'])) {
            return array('msg' => lang("contact_can't_be_empty"), 'status' => 2);
        }
        if (isset($data['co_id']) && $data['co_id']) {
            $infos = Db::name('company')->where('id', $data['co_id'])->value('status');
            if ($infos == 'LOCK') {
                return array('msg' => $data['name'] . lang("status_locked_can't_be_edit"), 'status' => 0);
            }
        }
        //判断邮箱
        foreach ($data['linkarr'] as $keys => $vals) {
            if ($vals['coattr_id'] == '3') {
                //邮箱
                if (!pregMAIL($vals['content'])) {
                    return array('msg' => lang("wrong_email_format"), 'status' => 0);
                    exit;
                }
            }
        }
        $admin = session('admin');
        $update_date = getTimeDate(null, 12);
        if ($data['sex'] == 1) {
            $sexman = lang("man");
        } else {
            $sexman = lang("woman");
        }
        if ($data['biaozhi'] == 'linkmanadd') {
            if (isset($data['isedit']) && $data['isedit'] == 1) {
                //数据的写入
                Db::startTrans();
                if($data['is_main'] == 'true'){
                    $data['is_main'] = '1';
                }elseif ($data['is_main'] == 'false'){
                    $data['is_main'] = '0';
                }
                if ($data['is_main'] == '1') {
                    //将之前的主账号变成非主要
                    $this->where('co_id', $data['co_id'])->setField('is_main', 0);
                }
                $data['create_id'] = $data['update_id'] = session('userid');
                $data['update_at'] = $data['create_at'] = time();
                $linkmanid = $this->strict(false)->insertGetId($data);//公司联系人表
                $datamethod['linkman_id'] = $linkmanid;//联系人id
                $datamethod['linkarr'] = $data['linkarr'];//联系方式
                //将数据写入到联系方式表
                $linkmethid = $this->addlinkmeth($datamethod, 1);
                if ($linkmanid && $linkmethid['status'] == 1) {
                    Db::commit();
                    return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
                } else {
                    return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
                }
            } else {
                return array(
                    'msg' => $data['name'] . lang("added_successfully"),
                    'status' => 1,
                    'update_at' => time(),
                    'update_date' => $update_date,
                    'update_id' => $admin['id'],
                    'updatename' => $admin['nickname'],
                    'sexman' => $sexman
                );
            }
        } elseif ($data['biaozhi'] == 'linkmanedit') {
            if (isset($data['isedit']) && $data['isedit'] == 1) {
                if($data['is_main'] == 'true'){
                    $data['is_main'] = '1';
                }elseif ($data['is_main'] == 'false'){
                    $data['is_main'] = '0';
                }
                //数据的写入
                if ($data['is_main'] == '1') {
                    //将之前的主账号变成非主要
                    $this->where('co_id', $data['co_id'])->setField('is_main', 0);
                }
                $updata['update_id'] = session('userid');
                $updata['update_at'] = time();
                $updata['name'] = $data['name'];
                $updata['position'] = $data['position'];
                $updata['is_main'] = $data['is_main'];
                $updata['sex'] = $data['sex'];
                Db::startTrans();
                $linkmanid = $this->where('id', $data['linkid'])->update($updata);//公司联系人表
                $datamethod['linkman_id'] = $data['linkid'];//联系人id
                $datamethod['linkarr'] = $data['linkarr'];//联系方式
                $linkmethid = $this->addlinkmeth($datamethod,1);
                if ($linkmanid !== false && $linkmethid['status'] == 1) {
                    Db::commit();
                    return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
                } else {
                    return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
                }
            } else {
                return array(
                    'msg' => $data['name'] . lang("edited_successfully"),
                    'status' => 1,
                    'update_at' => time(),
                    'update_date' => $update_date,
                    'update_id' => $admin['id'],
                    'updatename' => $admin['nickname'],
                    'sexman' => $sexman
                );
            }
        }
    }

    //联系方式的写入
    public function addlinkmeth($datamethod, $code)
    {
        if ($code == 1) {
            //公司联系方式写入
            Db::name('linkman_method')->where('linkman_id', $datamethod['linkman_id'])->delete();
            foreach ($datamethod['linkarr'] as $datamethkey => $datamethval) {
                $datamethval['linkman_id'] = $datamethod['linkman_id'];//联系人id
                $datamethval['create_id'] = $datamethval['update_id'] = session('userid');
                $datamethval['create_at'] = $datamethval['update_at'] = time();
                if ($datamethval['id']) {
                    Db::name('linkman_method')->strict(false)->insert($datamethval);
                } else {
                    Db::name('linkman_method')->strict(false)->insert($datamethval);
                }
            }
            $reslinkmanid = 1;
        } else {
            //个人联系方式写入
            foreach ($datamethod['linkmethods'] as $datamethkey => $datamethval) {
                $datamethval['admin_id'] = $datamethod['linkman_id'];//用户id
                if ($datamethval['id']) {
                    Db::name('adminattr_value')->strict(false)->insert($datamethval);
                } else {
                    Db::name('adminattr_value')->strict(false)->insert($datamethval);
                }
            }
            $reslinkmanid = 1;
        }
        if ($reslinkmanid) {
            return array('msg' => '', 'status' => 1);
            exit;
        } else {
            return array('msg' => '', 'status' => 0);
            exit;
        }
    }

    //联系人的删除
    public function dellink($data)
    {
        $dliname = getinfo($data['linkid'], 'linkman')['name'];
        Db::startTrans();
        $retlink = $this->where('id', $data['linkid'])->delete();//联系人表
        $linkmeth = Db::name('linkman_method')->where('linkman_id', $data['linkid'])->delete();
        if ($retlink && $linkmeth) {
            Db::commit();
            return array('msg' => $dliname . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $dliname . lang("failed_to_delete"), 'status' => 0);
        }
    }
}